﻿#include "GUILoadMonopoleDialog.h"
#include "ui_GUILoadMonopoleDialog.h"
#include "FITK_Component/FITKAcousticsModel/FITKAbstractSEASubSys.h"

#include "FITK_Component/FITKAcousticsPhysics/FITKLoadExcitationMonopole.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbstractSpectra.h"

namespace GUI {
    GUILoadMonopoleDialog::GUILoadMonopoleDialog(Core::FITKActionOperator* oper, QWidget* parent /*= nullptr*/) :
        GUILoadBaseDialog(nullptr, oper, parent),
        _ui(new Ui::GUILoadMonopoleDialog)
    {
        _ui->setupUi(this);
        // 设置对话框关闭时自动删除
        setAttribute(Qt::WA_DeleteOnClose);
        //去掉问号
        Qt::WindowFlags flags = windowFlags();
        flags &= ~Qt::WindowContextHelpButtonHint;
        setWindowFlags(flags);
        //初始化界面组件
        _ui->lineEdit_XLocation->setValidator(new QRegExpValidator(QRegExp("^(\\d+(\\.\\d+)?)$")));
        _ui->lineEdit_YLocation->setValidator(new QRegExpValidator(QRegExp("^(\\d+(\\.\\d+)?)$")));
        _ui->lineEdit_ZLocation->setValidator(new QRegExpValidator(QRegExp("^(\\d+(\\.\\d+)?)$")));
        //创建新载荷数据
        this->initWidgetImage();
        this->createNewLoadMonopoleObjData();
        this->init();
    }

    GUILoadMonopoleDialog::GUILoadMonopoleDialog(Acoustics::FITKAcousticsAbstractLoad* obj, Core::FITKActionOperator* oper, QWidget* parent /*= nullptr*/) :
        GUILoadBaseDialog(obj, oper, parent),
        _ui(new Ui::GUILoadMonopoleDialog)
    {
        _ui->setupUi(this);
        // 设置对话框关闭时自动删除
        setAttribute(Qt::WA_DeleteOnClose);
        //去掉问号
        Qt::WindowFlags flags = windowFlags();
        flags &= ~Qt::WindowContextHelpButtonHint;
        setWindowFlags(flags);
        //初始化界面组件
        _ui->lineEdit_XLocation->setValidator(new QRegExpValidator(QRegExp("^(\\d+(\\.\\d+)?)$")));
        _ui->lineEdit_YLocation->setValidator(new QRegExpValidator(QRegExp("^(\\d+(\\.\\d+)?)$")));
        _ui->lineEdit_ZLocation->setValidator(new QRegExpValidator(QRegExp("^(\\d+(\\.\\d+)?)$")));
        //初始化 
        this->initWidgetImage();
        this->init();
    }

    GUILoadMonopoleDialog::~GUILoadMonopoleDialog()
    {
        //释放数据
        if (_ui) {
            delete _ui;
            _ui = nullptr;
        }
    }

    void GUILoadMonopoleDialog::init()
    {
        if (!_obj) return;
        //初始化数据
        QString name = _obj->getDataObjectName();
        if (name.isEmpty())
        {
            name = "Monopole-1";
            name = this->ckeckNameFromMgr(name);
        }
        QString connectionName = _obj->getConnectionObjName();
        bool isEnabledState = _obj->getEnabledState();
        _ui->lineEdit_Name->setText(name);
        _ui->lineEdit_Connection->setText(connectionName);
        _ui->checkBox_EnabledState->setCheckState((isEnabledState ? Qt::CheckState::Checked : Qt::CheckState::Unchecked));
        double location[3] = { 0,0,0 };
        _obj->getConnectionLocation(location);
        _ui->lineEdit_XLocation->setText(QString::number(location[0]));
        _ui->lineEdit_YLocation->setText(QString::number(location[1]));
        _ui->lineEdit_ZLocation->setText(QString::number(location[2]));
        //获取类型
        bool isSeaModel = false;
        int seaType = 0;
        if (!this->getPickDiscDataType(_obj->getConnectionObjID(), isSeaModel, seaType)) return;
        //初始化相应的类型数据
        if (seaType == Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEACavity)
            this->initMonopoleSEAAcousticData();
    }

    void GUILoadMonopoleDialog::on_pushButton_Cancel_clicked()
    {
        //点击取消按键动作
        this->reject();
    }

    void GUILoadMonopoleDialog::on_pushButton_OK_clicked()
    {
        if (!_oper)return;
        //获取操作器动作类型
        QString operType;
        _oper->argValue<QString>("OperModelType", operType);
        if (operType.isEmpty())
        {
            delete _obj;
            this->reject();
            return;
        }
        //创建动作
        if (operType == "Create")
        {
            if (!this->createObjData())
                return;
        }
        //修改动作
        else if (operType == "Edit")
        {
            if (!this->editObjData())
                return;
        }
        this->accept();
    }

    void GUILoadMonopoleDialog::initWidgetImage()
    {
        //初始化图像
        _ui->label_Image->setPixmap(QPixmap(":/ToolBar/toolBar/Power.png"));
    }

    void GUILoadMonopoleDialog::initMonopoleSEAAcousticData()
    {
        //初始化下拉数据
        QList<QComboBox*> comboBoxList;
        QList<int> typeList;
        comboBoxList << _ui->comboBox_Magnitude;
        typeList << Acoustics::FITKAcousticsAbstractSpectra::AcousticsSpectraType::Power << Acoustics::FITKAcousticsAbstractSpectra::AcousticsSpectraType::Pressure;
        this->setComboBoxSpectra(comboBoxList, typeList);
        //初始化数据
        Acoustics::FITKLoadExcitationMonopole* loadData = dynamic_cast<Acoustics::FITKLoadExcitationMonopole*>(_obj);
        if (!loadData) return;
        int spectraID = loadData->getSpectraDataID();
        int index = _ui->comboBox_Magnitude->findData(spectraID);
        _ui->comboBox_Magnitude->setCurrentIndex((index < 0 ? 0 : index));
    }

    void GUILoadMonopoleDialog::createNewLoadMonopoleObjData()
    {
        //获取拾取数据
        if (!_oper) return;
        int pickObjID = 0;
        if (!_oper->argValue<int>("PickObjID", pickObjID)) return;
        double connectionLocation[3]{ 0.0,0.0,0.0 };
        if (!_oper->argValue<double>("connectPointX", connectionLocation[0])) return;
        if (!_oper->argValue<double>("connectPointY", connectionLocation[1])) return;
        if (!_oper->argValue<double>("connectPointZ", connectionLocation[2])) return;
        //获取拾取类型
        bool isSeaModel = false;
        int seaType = 0;
        if (!this->getPickDiscDataType(pickObjID, isSeaModel, seaType)) return;
        //创建新的数据
        _obj = new Acoustics::FITKLoadExcitationMonopole;
        if (!_obj) return;
        //设置连接器对象
        _obj->setConnectionObjID(pickObjID);
        _obj->setConnectionLocation(connectionLocation);
    }

    void GUILoadMonopoleDialog::getDataFormWidget()
    {
        if (!_obj) return;
        bool isEnabledState = _ui->checkBox_EnabledState->checkState() == Qt::CheckState::Checked;
        _obj->setEnabledState(isEnabledState);
        double location[3] = { 0,0,0 };
        location[0] = _ui->lineEdit_XLocation->text().toDouble();
        location[1] = _ui->lineEdit_YLocation->text().toDouble();
        location[2] = _ui->lineEdit_ZLocation->text().toDouble();
        _obj->setConnectionLocation(location);
        //获取类型
        bool isSeaModel = false;
        int seaType = 0;
        if (!this->getPickDiscDataType(_obj->getConnectionObjID(), isSeaModel, seaType)) return;
        //初始化相应的类型数据
        if (seaType == Acoustics::FITKAbstractSEASubSys::SEASubSysType::SEACavity)
            this->getMonopoleSEAAcousticDataFormWidget();
    }

    void GUILoadMonopoleDialog::getMonopoleSEAAcousticDataFormWidget()
    {
        Acoustics::FITKLoadExcitationMonopole* loadData = dynamic_cast<Acoustics::FITKLoadExcitationMonopole*>(_obj);
        if (!loadData) return;
        //从窗口获取数据
        int spectraID = _ui->comboBox_Magnitude->currentData().toInt();
        //设置数据
        loadData->setSpectraDataID(spectraID);
    }

    QString GUILoadMonopoleDialog::getNameFormWidget()
    {
        return _ui->lineEdit_Name->text();
    }
}
